﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      <a-radio-group
        v-model="queryParam[0].keyword"
        style="margin-left: 8px"
        :default-value="null"
        button-style="solid"
        @change="getDataList()"
      >
        <a-radio-button :value="null">全部</a-radio-button>
        <a-radio-button v-for="item in this.GiftTypes" :key="item.DicKey" :value="item.DicName"> {{ item.DicName }}</a-radio-button>
      </a-radio-group>
    </div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam[1].keyword" placeholder="客户名称" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button
            >
            <a-button style="margin-left: 8px" @click="Repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      :scroll="{ y: tableHeight }"
      :columns="columns"
      :rowKey="(row, index) => index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
      <span slot="ShouldQuantity" slot-scope="ShouldQuantity" v-format="'#,##0'">{{ ShouldQuantity }}</span>
      <span slot="ReturnedQty" slot-scope="ReturnedQty" v-format="'#,##0'">{{ ReturnedQty }}</span>
      <span slot="NotiQuantity" slot-scope="NotiQuantity" v-format="'#,##0'">{{ NotiQuantity }}</span>
      <span slot="SSMdDate" slot-scope="SSMdDate">{{ SSMdDate?timeFormat(SSMdDate,'YYYY-MM-DD'):'' }}</span>
      <span slot="RDdDate" slot-scope="RDdDate">{{ RDdDate?timeFormat(RDdDate,'YYYY-MM-DD'):'' }}</span>
    </a-table>
  </a-card>
</template>

<script>
const columns = [
  { title: '客户编码', dataIndex: 'cCusCode', align: 'center', width: '8%' },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left', width: '20%', ellipsis: true },
  { title: '客户经理', dataIndex: 'PrincipalName', align: 'center', width: '5%' },
  { title: '返料类型', dataIndex: 'GiftType', align: 'center', width: '5%' },
  {
    title: '返料品名',
    dataIndex: 'GiftPId',
    align: 'center',
    width: '15%',
    customRender: (text, record) => {
      if (text) {
        return record.GiftPId + '/' + record.GiftPName
      } else {
        return '无返料信息'
      }
    }
  },
  { title: '应返数', dataIndex: 'ShouldQuantity', align: 'right', width: '8%', scopedSlots: { customRender: 'ShouldQuantity' } },
  { title: '已返数', dataIndex: 'ReturnedQty', align: 'right', width: '8%', scopedSlots: { customRender: 'ReturnedQty' } },
  { title: '欠返数', dataIndex: 'NotiQuantity', align: 'right', width: '8%', scopedSlots: { customRender: 'NotiQuantity' } },
  { title: '最后交易日期', dataIndex: 'SSMdDate', align: 'center', width: '8%', scopedSlots: { customRender: 'SSMdDate' } },
  { title: '最后返料日期', dataIndex: 'RDdDate', align: 'center', width: '8%', scopedSlots: { customRender: 'RDdDate' } }
]

export default {
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true) + 24
    })
    this.getGiftTypes()
    this.getDataList()
  },
  data () {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      columns,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      GiftTypes: [],
      time: null,
      filters: {},
      sorter: { field: 'cCusCode', order: 'desc' },
      loading: false,
      queryParam: [{ condition: 'GiftType' }, { condition: 'cCusName' }]
    }
  },
  methods: {
    Repossess () {
      this.queryParam = [{ condition: 'GiftType', keyword: null }, { condition: 'cCusName' }]
    },
    getGiftTypes () {
      this.$http
        .post('/Base_BasicData/Base_DictionaryMain/GetTheDataByDicKey', {
          Condition: 'DicKey',
          Keyword: 'MaterialReturnType'
        })
        .then(res => {
          this.GiftTypes = res.Data.DictionaryDetailsList
        })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/GiftManagement/GMM_GiftMaterialApplyMain/GetDataListForGMMLedgerByCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
  }
}
</script>
